import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class SocketTest {
    public static class GreetingServer extends Thread {
        private ServerSocket serverSocket;

        public GreetingServer(int port) throws IOException {
            serverSocket = new ServerSocket(port);
            serverSocket.setSoTimeout(10000);
        }

        public void run() {
            System.out.println("等待远程连接，端口号为：" + serverSocket.getLocalPort() + "...");
            Socket server = null;
            try {
                server = serverSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("远程客户端地址：" + server.getRemoteSocketAddress());
            while(true) {
                try {
                    DataInputStream in = new DataInputStream(server.getInputStream());
                    String str = in.readUTF();
                    System.out.println("客户端发送:"+str);
                    DataOutputStream out = new DataOutputStream(server.getOutputStream());
                    if (str.equals("exit")){
                        out.writeUTF("已关闭：" + server.getLocalSocketAddress() + "\nGoodbye!");
                        out.flush();
                        server.close();
                        break;
                    }
                    else{
                        out.writeUTF("hello world");
                        out.flush();
                    }
                }catch(SocketTimeoutException s) {
                    System.out.println("Socket timed out!");
                    break;
                }catch(IOException e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
    }
    public static class Client implements Runnable{
        private Socket socket;
        private int port;
        public Client(int port) throws IOException {
            socket = new Socket("localhost", port);
            this.port=port;
        }
        public void run(){
            System.out.println("连接到主机：,端口号："+port);
            System.out.println("远程服务器地址：" + socket.getRemoteSocketAddress());
            while (true){
                System.out.println("客户端输入信息:");
                BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
                OutputStream outToServer;
                try {
                    outToServer = socket.getOutputStream();
                    DataOutputStream out = new DataOutputStream(outToServer);
                    String str = sin.readLine();
                    out.writeUTF(str);
                    outToServer.flush();
                    InputStream inFromServer = socket.getInputStream();
                    DataInputStream in = new DataInputStream(inFromServer);
                    System.out.println("服务器响应： " + in.readUTF());
                    if(str.equals("exit")){
                        socket.close();
                        break;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String [] args) throws IOException {
        Thread t1 = new GreetingServer(8989);
        t1.start();
        Thread t2=new Thread(new Client(8989));
        t2.start();
    }
}
